package com.lbd.gulimall.product.app;

import java.util.Arrays;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lbd.common.validate.AddGroup;
import com.lbd.common.validate.UpdateGroup;
import com.lbd.gulimall.product.feign.MemberFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lbd.gulimall.product.entity.BrandEntity;
import com.lbd.gulimall.product.service.BrandService;
import com.lbd.common.utils.PageUtils;
import com.lbd.common.utils.R;


/**
 * 品牌
 *
 * @author linbd
 * @email 2236000738@qq.com
 * @date 2021-06-04 17:54:12
 */
@RestController
@RequestMapping("product/brand")
@Slf4j
@RefreshScope//动态获取配置中心刷新的值
public class BrandController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private MemberFeignService memberFeignService;
    @Value("${product.name}")
    private String productName;
    @Value("${product.price}")
    private String productPrice;

    @RequestMapping("test")
    public R test() {
        R member = memberFeignService.test();
        BrandEntity brandEntity = brandService.getOne(new QueryWrapper<BrandEntity>().eq("name", "华为"));
        log.info("用户信息：{}",member.get("memberInfo"));
        log.info("品牌信息：{}",brandEntity);
        log.info("额外配置信息：name->{},price->{}",productName,productPrice);
        return R.ok().put("memberInfo",member.get("memberInfo")).put("brandInfo",brandEntity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated(value = AddGroup.class) @RequestBody BrandEntity brand){
		brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated(value = UpdateGroup.class) @RequestBody BrandEntity brand){
        //修改分类和品牌时都要级联同步中间表数据的冗余字段
		brandService.updateDetail(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
